<?php
	include_once "/home/mmediaai/www/ad/sys/com/model/util/PHPMailer/class.phpmailer.php";	
		
	
	header("Content-Type:text/html; charset=utf-8");
	/**
	 * Sending Email
	 * @param unknown_type $send
	 * @param unknown_type $subject
	 * @param unknown_type $content
	 */
	function email($sends, $subject, $content) {
		$mail = new PHPMailer();
		$mail->IsSMTP();                  // set mailer to use SMTP
		$mail->Host = "mail.mmedia.ai-ad.net.tw";  // specify main and backup server
		$mail->SMTPAuth = true;     // turn on SMTP authentication
		//$mail->SMTPSecure = "ssl";  //use ssl
		$mail->Username = "service@mmedia.ai-ad.net.tw";  // SMTP username
		$mail->Password = "service"; // SMTP password
		$mail->Port = 25;
		$mail->CharSet="utf-8";
		$mail->From = "service@mmedia.ai-ad.net.tw";
		$mail->FromName = "Amazing Mobile ADs";
		
		if (is_array($sends)) {
			foreach($sends as $send) {
				$mail->AddAddress($send);
			}
		} else {
			$mail->AddAddress($sends);
		}
		//$mail->AddAddress("ellen@example.com");                  // name is optional
		//$mail->AddReplyTo("info@example.com", "Information");
		//$mail->WordWrap = 50;                                 
		//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
		$mail->IsHTML(true);                                  // set email format to HTML
		$mail->Subject = $subject;
		$mail->Body = $content;
    	return $mail->Send();
	}
	
	 
	 
	include_once '/home/mmediaai/www//ad/sys/com/model/db/DBConn.php';	
	
	$conn = new DBConn ();
	
	 
	//查詢 媒體商及一般使用者 每日預算通知
	 $sql = "select * from user where (type=1 or type =2) and status =1 and NotifyBudgetUpdate=1";
	 $result = mysql_query($sql);
	
	 $row_set = array ();
 
	 date_default_timezone_set('Asia/Taipei');
	 
	 $nowDate = date("Y/m/d");
	   
	while ($row = @mysql_fetch_object ( $result ) ) {
		$row_set [] = $row;	 
	}
	
    
	$user = null;
 
	
	for ( $i=0 ,$length = count($row_set); $i < $length ; $i++ ) {
  		//$user = $row_set[$i];
  	 
 		$acocunt =  $row_set[$i]->Account;
	    $name =  $row_set[$i]->Name;
	 	$sql = " select 
				(select count(id) * (select CPM from paysetting )  from clickedad where status = 0  and date_format(CreateTime, '%Y/%m/%d') = date_format(c.CreateTime, '%Y/%m/%d') and adid =a.id) +
				(select count(id) * (select CPC from paysetting )  from clickedad where status = 1  and date_format(CreateTime, '%Y/%m/%d') = date_format(c.CreateTime, '%Y/%m/%d') and adid =a.id) as pay , 
				(select count(id) * (select CPM from paysetting )  from clickedad where status = 0  and adid =a.id) +
				(select count(id) * (select CPC from paysetting )  from clickedad where status = 1  and adid =a.id) as sumPay  
				,a.*
				from clickedad c, ad a 
				where c.adId = a.id and a.account = '$acocunt'
				and date_format(c.createTime, '%Y/%m/%d') = date_format(now(), '%Y/%m/%d')
				group by a.id ";
	    
	 	$result = mysql_query($sql);	 
		
	 	while ($ad = @mysql_fetch_object ( $result ) ) {
		   	email("$acocunt", 
 			  "$ad->Title 廣告花費通知",
 			  "親愛的  $name  您好， <BR><BR>

			        感謝您購買Amazing Mobile Ads廣告， <BR>
			        您的活動 $ad->Title ，於 $nowDate 共花費 : $ad->pay  元   <BR> 
			        此廣告累積花費  $ad->sumPay 元。<BR><BR>  
			        如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw <BR>
			        謝謝您， <BR> <BR>
			   Amazing Mobile Ads敬上
 			  "
 			);
		}
	}
	 
	 @mysql_free_result ( $result );
	 @mysql_close();
?>